﻿using System;
using System.Collections.Generic;
using Component.Tools;
using Component.Track;
using Component.Trains;
using Component.UI.Entity;
using Component.UI.View;
using Config;
using TMPro;
using Tools;
using UnityEngine;

namespace Component.UI
{
    public class GameController : MonoBehaviour
    {
        private float timePower = 1;
        private List<StageEntity> stages;
        private StageView[] views;

        private int currentStageIndex;
        private int score;
        private Vector3 currentTarget;
        private readonly string[] days = new[] {"周一","周二","周三","周四","周五","周六","周日",};
        private int currentDay = 0;
        private ClockTools clockTools;
        private StationManager startViewStation;

        public delegate void OnStartGame();
        public OnStartGame onStartGame { get; set; }
        
        [Header("结算界面")]
        [SerializeField] private ResultView resultView;
        [Header("时钟指针")]
        [SerializeField] private Transform pointer;

        
        [SerializeField] private GameObject viewsParent;
        [SerializeField] private TextMeshProUGUI dayText;
        [SerializeField] private TextMeshProUGUI scoreText;
        
        
        private int runDay;
        private int take;

        public int RunDay
        {
            get => runDay;
            set => runDay = value;
        }

        public int Take
        {
            get => take;
            set => take = value;
        }

        public Transform CurrentTargetPos => views[currentStageIndex].TargetTransform;

        private void Awake()
        {

            stages = FileTools<StageEntity>.ReadJsonData(DataHelper.STAGE_FILE_NAME);

            views = viewsParent.GetComponentsInChildren<StageView>();

            for (int i = 0; i < views.Length; i++)
            {
                views[i].InitView(stages[i]);
            }

        }

        private void Start()
        {
        }

        public void ResetGame()
        {
            Line.ResetAllLine();
            TrainManager.ResetAllTrain();
            CameraTools.ResetCameraSize();
            StationGeneration.Instance.StopGenerate();
            //替换为界面的StationManager，并继续Generate.
            StationGeneration.Instance = startViewStation;
            StationGeneration.Instance.ContinueGenerate();
            clockTools.Reset();
        }

        public void OnStageSelected(int index)
        {
            currentStageIndex = index;
        }
        
        public void StartGame()
        {
            
            //移动视角至关卡，并初始化StationGeneration
            CameraTools.CameraFollow(views[currentStageIndex].TargetTransform.transform.position, 1, Vector2.zero, delegate
            {
                CameraTools.CameraSizeFollow(7,1);
            }, delegate
            {
                CameraTools.SetBounds(new Vector3(3,3,3));
                
                ViewManager.GetIntoStartView(ViewManager.ViewType.GameView);

                startViewStation = StationGeneration.Instance;
                StationGeneration.Instance.PauseGenerate();
                StationGeneration.Instance = views[currentStageIndex].Target;
                StationGeneration.Instance.StartGenerate();
                
                onStartGame.Invoke();
                clockTools = new ClockTools(pointer);
                clockTools.ContinueRunning();

                score = 0;
                scoreText.text = score.ToString();
                
                clockTools.DayLaterEvent = delegate(bool cycle)
                {
                    if (cycle)
                    {
                        dayText.text = days[++currentDay % 7];
                    }
                };
            });
            
        }

        private bool gameOver;
        public void GameOverFocus(Vector3 disableStation)
        {
            if (gameOver)
            {
                return;
            }

            gameOver = !gameOver;
            resultView.InitGameOverView(disableStation, runDay, take);
            GameOver();
        }

        public void GameOver()
        {
            StationGeneration.Instance.PauseGenerate();
            TrainManager.PauseRunning();
            clockTools.PauseRunning();
        }

        public void GamePause()
        {
            CameraTools.ResetCameraSize(5);
            //站点控制
            StationGeneration.Instance.PauseGenerate();
            //火车控制
            TrainManager.PauseRunning();
            //时钟暂停
            clockTools.PauseRunning();
        }
        
        public void GameUnPause()
        {
            StationGeneration.Instance.ContinueGenerate();
            TrainManager.ContinueRunning();
            clockTools.ContinueRunning();
        }
        
        /// <summary>
        /// 乘客到站获得奖励
        /// </summary>
        public void PassengerArrival()
        {
            score++;
            scoreText.text = score.ToString();
        }

    }

    public class GameManager
    {
        private static GameController instance;

        public static GameController Instance => instance;

        static GameManager()
        {
            instance = GameObject.FindWithTag("GameController").GetComponent<GameController>();
        }
    }
    
}